package com.eclecticconceptions.vulcan;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TightnessActivity extends Activity {

	TextView lblTimer, txtTestInfo;
	static GasCountDown testTimer;
	String strInfo;
	long testLength;
	int testCode=0, testStatus=0; //0 is not running, 1 is letby running, 2 is tightness running
	Button btnLetBy, btnTightness;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	    setContentView(R.layout.tabtightness);
	    lblTimer = (TextView) findViewById(R.id.lblCountdownTimer);
	    btnLetBy=(Button)findViewById(R.id.btnLetBy);
	    btnTightness=(Button)findViewById(R.id.btnTightness);
	    txtTestInfo=(TextView)findViewById(R.id.txtTestInfo);
	}
	/*@Override
	public void onConfigurationChanged(Configuration newConfig) {
	  super.onConfigurationChanged(newConfig);
	  setContentView(R.layout.tabtightness);
	  lblTimer = (TextView) findViewById(R.id.lblCountdownTimer);
	    btnLetBy=(Button)findViewById(R.id.btnLetBy);
	    btnTightness=(Button)findViewById(R.id.btnTightness);
	    txtTestInfo=(TextView)findViewById(R.id.txtTestInfo);
	}
	*/
	
	public void btnLetByClick (View view){
		/**set test time to 1min,
		 * hide tightness button and change text on btnLetBy to reset
		 * change timerStatus to 1 (running)
		 * call controlTest
		 */
		if (testStatus == 0 ){
		testCode=1; //to tell controlTest what test to run
		btnTightness.setVisibility(View.INVISIBLE);
		btnLetBy.setText(R.string.Reset);
		
		} else {
			testCode=0;
			btnTightness.setVisibility(0);
			btnLetBy.setText(R.string.btnLetBy);
		}
		controlTest();
	}
	
	public void btnTightnessClick(View view){
		/**set test time to 2min,
		 * hide letby button and change text on btnTightness to reset
		 * change timerStatus to 1 (running)
		 * call controlTest
		 */
		if (testStatus == 0 ){
		testCode=2; //to tell controlTest what test to run
		btnLetBy.setVisibility(View.INVISIBLE);
		btnTightness.setText(R.string.Reset);
		
		} else {
			testCode=0;
			btnLetBy.setVisibility(0);
			btnTightness.setText(R.string.btnTightness);
		}
		controlTest();
		
	}

	public void controlTest (){
		switch(testStatus){
		case 0: //nothing currently running
			if(testCode==1){
				testLength=60000;
				strInfo="1 min Let By Test in progress.";
			} else {
				testLength=120000;
				strInfo="2 min Tightness Test in progress.";
			}
			testTimer = new GasCountDown(testLength,1000,lblTimer);
			testTimer.start();
			testStatus=1; //set status to running
			txtTestInfo.setText(strInfo);
			break;
		case 1: //a test is running so reset everything
			testTimer.cancel();
			lblTimer.setText("0:00");
			testStatus=0;
			txtTestInfo.setText("");
			break;
		}
		
		return;
	}
	
	public static class GasCountDown extends CountDownTimer {
				
		TextView txtCounter;
		
		public GasCountDown(long millisUntilFinished, long interval, TextView txtView) {
			super(millisUntilFinished,interval);
			txtCounter=txtView;
		}

		@Override
		public void onFinish() {
			txtCounter.setText("Done");
			
		}

		@Override
		public void onTick(long millisUntilFinished) {
			txtCounter.setText(Cp12Utils.formatTime((int) millisUntilFinished / 1000));
			
		}
		
	}
}
